﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;

namespace NewsWorm {
    internal class TreeViewState {
        internal TreeViewState( string basepath ) {
            m_Path = basepath;
        }

        internal void Save( TreeView view ) {
            try {
                StreamWriter writer = new StreamWriter( m_Path + "expanded.dat", false, Encoding.Default );

                foreach ( TreeNode node in view.Nodes ) {
                    if ( node.IsExpanded )
                        writer.WriteLine( node.Name );
                }

                writer.Close();
            } catch ( Exception e ) {
                new MyMessageBoxError( e.Message, e.StackTrace ).ShowDialog();
            }
        }

        internal List<string> LoadState() {
            List<string> nodes = new List<string>();
            string file = m_Path + "expanded.dat";

            if ( File.Exists( file ) ) {
                StreamReader reader = new StreamReader( file, Encoding.Default );
                string line;

                while ( ( line = reader.ReadLine() ) != null )
                    nodes.Add( line );

                reader.Close();
            }

            return nodes;
        }

        private string m_Path;

    }
}
